#!/bin/bash
LINKQUOTA="/${QUOTA_TEST}/linkQuotaDir"
LINKDIR="${FS_MOUNTPOINT}${LINKQUOTA}"
CHILDDIR="${LINKDIR}/childdir"
LOGFILE="/tmp/quotalog"
global_used=0

msg0="测试所有用例";                   
msg1="测试新建 softlink 且不超出配额";  
msg2="测试新建 softlink 且超出配额";
msg3="测试在 <同一个目录> 下新建 hardlink, 肯定不会超出配额";
msg4="测试在 <不同目录> 下新建 hardlink , 不允许创建";
msg5="测试删除软链接文件，正确减少配额用量";
msg6="测试删除硬链接文件, 删除文件硬链接时配额容量不变, 删除真正文件时配额用量正确减少";
msg_num=6

function print_help() {
    name="$0"
    echo "Usage:"
    for (( num=0; num<=${msg_num}; num++ ));do
        eval echo "'  'sh ${name} ${num} '    ' \${msg${num}}"
    done
    exit
}

function init_all() {
    hard_limit=$(($1 * 1024 * 1024)) #10 MB
    clean_quota; #sleep ${WAIT_CLIENT_SYNC};
    rm -f ${LINKDIR}/*; mkdir -p ${LINKDIR}; echo -e "=====清空目录===== pass"

    orcafs-ctl --setquota --dir=${LINKDIR} --hardlimit=${hard_limit} &> /dev/null
    sleep ${WAIT_CLIENT_SYNC};
    
    echo -n "=====新创建文件然后重新创建配额 (hardlimit=$1 MB)===== "
    quota_num=`orcafs-ctl --listquota | grep -w DIR | grep ${LINKQUOTA} | wc -l`
    if ((${quota_num} == 1));then echo "pass"; else echo "not pass"; exit 1; fi; echo "";
}

function test_softlink() {
    echo "测试新建文件写 $1 MB 数据后对其创建软链接 (不超出配额)"
    file_size=$(($1 * 1024 * 1024))
    rm -rf ${LINKDIR}/*
    dd if=/dev/zero of=${LINKDIR}/file bs=${file_size} count=1 &> /dev/null
    func_get_quota_used; old_used=${global_used}

    cmd="ln -s ${LINKDIR}/file ${LINKDIR}/s_link";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd}
    link_size=`stat -c "%s" ${LINKDIR}/s_link`

    func_get_quota_used; new_used=${global_used}
    expect_used=$((${old_used} + ${link_size}))

    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} == ${file_size})) && (( ${new_used} == ${expect_used})) && ((${link_size} > 0)); then 
        echo -e "success\n"; else echo -e "fail\n"; exit 1; fi
}

function test_softlink_exceed() {
    echo "测试新建文件写 $1 MB 数据后对其创建软链接 (超出配额)"
    rm -rf ${LINKDIR}/*
    file_size=$(($1 * 1024 * 1024))
    dd if=/dev/zero of=${LINKDIR}/file bs=${file_size} count=1 &> /dev/null
    func_get_quota_used; old_used=${global_used}

    cmd="ln -s ${LINKDIR}/file ${LINKDIR}/s_link";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &> ${LOGFILE}
    exceed=`cat ${LOGFILE} | grep "Disk quota exceeded" | wc -l`
        if (( ${exceed} == 1 )); then echo -e "\t Failed to create symbolic link: Disk quota exceeded"; fi

    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} == ${file_size})) && (( ${new_used} == ${file_size})) && (( ${exceed} == 1 )); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
}

function test_hardlink_samedir() {
    link_name="hard_link"
    file_size=10 #M
    quota_size=$((${file_size}*1024*1024))
    echo "测试新建文件写 ${file_size} MB 数据后 <在同一个目录> 对其创建硬链接 (硬链接不增加文件系统大小)"

    rm -rf ${LINKDIR}/*
    dd if=/dev/zero of=${LINKDIR}/file bs=${file_size}M count=1 &> /dev/null
    func_get_quota_used; old_used=${global_used}

    cmd="ln ${LINKDIR}/file ${LINKDIR}/${link_name}";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd}
    link_size=`stat -c "%s" ${LINKDIR}/${link_name}`

    func_get_quota_used; new_used=${global_used}
    expect_used=$((${old_used} + ${link_size}))

    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} == ${quota_size})) && (( ${new_used} == ${quota_size})) &&\
        [ -e ${LINKDIR}/${link_name} ] && ((${link_size} == ${quota_size})); then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
}

function test_hardlink_diffdir() {
    link_name="hard_link"
    file_size=10 #M
    quota_size=$((${file_size}*1024*1024))
    echo "测试新建文件写 ${file_size} MB 数据后 <在不同一个目录> 对其创建硬链接 (不能在不同目录创建硬链接)"

    rm -rf ${LINKDIR}/*
    mkdir -p ${CHILDDIR}
    dd if=/dev/zero of=${LINKDIR}/file bs=${file_size}M count=1 &> /dev/null
    func_get_quota_used; old_used=${global_used}

    cmd="ln ${LINKDIR}/file ${CHILDDIR}/${link_name}";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd} &>${LOGFILE}
    echo -e "\t$(cat ${LOGFILE})"
    res=`grep "Operation not permitted" ${LOGFILE}`
    if [ -n "${res}" ] ; then
        echo -e "\tCan't create hardlink in different directory, 测试成功\n"; break; else echo "验证失败"; exit 1;
    fi

    func_get_quota_used; new_used=${global_used}

    eval echo -n -e "==${test_num}==\${msg$test_num}==== "
    if ((${old_used} == ${quota_size})) && (( ${new_used} == ${quota_size})) &&\
        [ ! -e ${CHILDDIR}/${link_name} ]; then 
        echo -e "验证成功"; else echo -e "验证失败"; exit 1; fi
}

function test_rm_softlink() {
    file_size=3 #M
    quota_size=$((${file_size}*1024*1024))
    echo "测试新建文件写 ${file_size} MB 数据后 对其创建软链接并删除"
    
    rm -rf ${LINKDIR}/*
    dd if=/dev/zero of=${LINKDIR}/file bs=${file_size}M count=1 &> /dev/null
    func_get_quota_used; old_used=${global_used}
    if ((${old_used} != ${quota_size})); then eval echo -e "==${test_num}==\${msg$test_num}====验证失败1"; exit 1; fi

    cmd="ln -s ${LINKDIR}/file ${LINKDIR}/s_link"; echo -e "exec cmd:\n\t${cmd}"; eval ${cmd}
    link_size=`stat -c "%s" ${LINKDIR}/s_link`
    if !((${link_size} > 0)); then echo -e "==${test_num}==\${msg$test_num}====验证失败2"; exit 1; fi

    func_get_quota_used; new_used=${global_used}
    expect_used=$((${old_used} + ${link_size}))
    if (( ${new_used} != ${expect_used})); then eval echo -e "==${test_num}==\${msg$test_num}====验证失败3"; exit 1; fi

    cmd="rm -f ${LINKDIR}/s_link";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd}
    func_get_quota_used; rm_used=${global_used}
    if (( ${rm_used} != ${quota_size})); then eval echo -e "==${test_num}==\${msg$test_num}====验证失败4"; exit 1; fi

    eval echo -e "==${test_num}==\${msg$test_num}==== 验证成功"
}

function test_rm_hardlink_samedir() {
    link_name="hard_link"
    file_size=10 #M
    quota_size=$((${file_size}*1024*1024))
    echo "测试新建文件写 ${file_size} MB 数据后 <在同一目录>对其创建硬链接并先后删除链接文件和源文件"

    rm -rf ${LINKDIR}/*
    dd if=/dev/zero of=${LINKDIR}/file bs=${file_size}M count=1 &> /dev/null
    func_get_quota_used; old_used=${global_used}
    if ((${old_used} != ${quota_size})); then echo -e "验证失败1"; exit 1; fi

    cmd="ln ${LINKDIR}/file ${LINKDIR}/${link_name}";echo -e "exec cmd:\n\t${cmd}"; eval ${cmd}
    link_size=`stat -c "%s" ${LINKDIR}/${link_name}`
    if [ -e ${LINKDIR}/${link_name} ] && ((${link_size} == ${quota_size})); then echo ""; else echo -e "验证失败2"; exit 1; fi

    func_get_quota_used; new_used=${global_used}
    expect_used=$((${old_used} + ${link_size}))
    if (( ${new_used} != ${quota_size})); then echo -e "验证失败3"; exit 1; fi

    cmd="rm -f ${LINKDIR}/${link_name}"; echo -e "exec cmd:\n\t${cmd}"; eval ${cmd}
    if [ -e ${LINKDIR}/${link_name} ];then echo -e "删除硬链接验证失败4"; exit 1; fi

    func_get_quota_used; rm_used=${global_used}
    if (( ${rm_used} != ${quota_size})); then echo -e "验证失败5"; exit 1; fi

    cmd="rm -f ${LINKDIR}/file"; echo -e "exec cmd:\n\t${cmd}"; eval ${cmd}
    if [ -e ${LINKDIR}/file ];then echo -e "删除文件验证失败5"; exit 1; fi

    func_get_quota_used; rm_used=${global_used}
    if (( ${rm_used} != 0)); then echo -e "验证失败6"; exit 1; fi

    eval echo -e "==${test_num}==\${msg$test_num}==== 验证成功"
}


echo "/*******************************"
echo "   测试 hardlink softlink       "
echo "*******************************/"
if [ "$1" == "--help" ] || [ -z "$1" ];then print_help; fi

# init global quota env and check env
sh ${QUOTA_CHECK_ENV}
source ${QUOTA_COMMON_FUNC}

init_all 10; # 10 MB
test_num=1; if (($1 == ${test_num})) || (($1 == 0));then test_softlink 3; fi
test_num=2; if (($1 == ${test_num})) || (($1 == 0));then test_softlink_exceed 10; fi
test_num=3; if (($1 == ${test_num})) || (($1 == 0));then test_hardlink_samedir; fi
test_num=4; if (($1 == ${test_num})) || (($1 == 0));then test_hardlink_diffdir; fi
test_num=5; if (($1 == ${test_num})) || (($1 == 0));then test_rm_softlink; fi
test_num=6; if (($1 == ${test_num})) || (($1 == 0));then test_rm_hardlink_samedir; fi

echo "/*******************************"
echo " 测试 hardlink/softlink 全部成功 "
echo "*******************************/"
exit 0;
